package company;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.jar.JarEntry;

public class MyFrame extends JFrame {

    Actor people=new Actor();
    Actor machine=new Actor();
    JButton attack=new JButton("攻击");
    JButton defense=new JButton("防御");
    JButton xuqi=new JButton("蓄气");
    JLabel life1=new JLabel("你的生命值"+people.life);
    JLabel power1=new JLabel("你的蓄气值"+people.power);
    JLabel life2=new JLabel("电脑生命值"+machine.life);
    JLabel power2=new JLabel("电脑蓄气值"+machine.power);
     JLabel thing1=new JLabel("你的行动为");
    JLabel thing2=new JLabel("电脑行动为");
    JLabel thing3=new JLabel("博弈结果为");
    mypicture ai=new mypicture();
    mypicture ai2=new mypicture();
    mypicture ai3=new mypicture();
    mypicture ai4=new mypicture();
    mypicture ai5=new mypicture();
    mypicture ai6=new mypicture();
    attack gong=new attack();
    defense fang=new defense();
    xuqi xu=new xuqi();
    attack gong1=new attack();
    defense fang1=new defense();
    xuqi xu1=new xuqi();
    JPanel root=new JPanel();
    JButton rule=new JButton("游戏规则");
    public MyFrame(String title) {
        super(title);
        root.setLayout(null);
        this.setContentPane(root);
        root.setBackground(Color.white);
        root.add(rule);
        rule.setBounds(210,100,140,30);
        root.add(xuqi);
        xuqi.setBounds(10,410,100,50);
        root.add(defense);
        defense.setBounds(210,410,100,50);
        root.add(attack);
        attack.setBounds(410,410,100,50);
        root.add(life1);
        life1.setBounds(20,40,100,50);
        root.add(ai);
        ai.setBounds(70,33,50,50);
        root.add(ai2);
        ai2.setBounds(100,33,50,50);
        root.add(ai3);
        ai3.setBounds(130,33,50,50);
        root.add(power1);
        power1.setBounds(20,80,100,50);
        root.add(life2);
        life2.setBounds(400,40,100,50);
        root.add(ai4);
        ai4.setBounds(450,33,50,50);
        root.add(ai5);
        ai5.setBounds(480,33,50,50);
        root.add(ai6);
        ai6.setBounds(510,33,50,50);
        root.add(power2);
        power2.setBounds(400,80,100,50);
        root.add(thing1);
        thing1.setBounds(20,120,250,50);
        root.add(thing3);
        thing3.setBounds(210,200,250,50);
        root.add(thing2);
        thing2.setBounds(400,120,250,50);
        rule.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(root,"波波攒游戏规则是首先两人要一起做好准备，\n" +
                        "选择三个行动方式，分别是攻击，蓄气，防御。\n" +
                        "★★攻击需要耗费一点蓄力值\n" +
                        "★★防御可以免受攻击的伤害\n" +
                        "★★蓄力可以增加一点蓄力值\n" +
                        "如果在蓄力的时候收到攻击，将会受到一点伤害\n" +
                        "当任何一方生命值归零时，游戏结束！！");
            }
        });
        attack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(people.power!=0){
                    int m_act=new randomw().random(machine.power);
                    people.attack();
                    thing1.setText("你发起了攻击，蓄气值-1");
                    root.remove(xu);
                    root.remove(fang);
                    root.repaint();
                    root.add(gong);
                    gong.setBounds(20,200,150,150);
                    root.repaint();
                    if(m_act==2){
                        machine.attack();
                        root.remove(fang1);
                        root.repaint();
                        root.remove(xu1);
                        root.repaint();
                        root.add(gong1);
                        root.repaint();
                        gong1.setBounds(430,200,150,150);
                        thing2.setText("电脑发起了攻击，蓄气值-1");
                        thing3.setText("你和电脑同时发起攻击，冲击波相互抵消");
                    }
                    else if(m_act==1){
                        machine.denfense();
                        root.remove(gong1);
                        root.repaint();
                        root.remove(xu1);
                        root.repaint();
                        root.add(fang1);
                        fang1.setBounds(430,200,150,150);
                        root.repaint();
                        thing2.setText("电脑开始了防御。");
                        thing3.setText("你的冲击波被电脑防御，攻击无效");
                    }
                    else if(m_act==0){
                        machine.xuqi();
                        root.remove(gong1);
                        root.remove(fang1);
                        root.add(xu1);
                        xu1.setBounds(430,200,150,150);
                        root.repaint();
                        thing2.setText("电脑使用了蓄气，蓄气值+1");
                        thing3.setText("你在电脑蓄气时攻击，电脑生命值-1");
                        machine.wood();
                        if(machine.life==2){
                            root.remove(ai6);
                            root.repaint();
                        }
                        if(machine.life==1){
                            root.remove(ai5);
                            root.repaint();
                        }
                    }
                }
                else{
                    root.remove(gong);
                    root.remove(fang);
                    root.remove(xu);
                    root.remove(gong1);
                    root.remove(fang1);
                    root.remove(xu1);
                    root.repaint();
                    thing1.setText("你的蓄气值不够，无法攻击");
                    thing2.setText("请重新行动");
                    thing3.setText("你会不会玩啊！");
                }
                fresh();
                if(machine.life==0){
                        root.remove(ai4);
                        root.repaint();
                    JOptionPane.showMessageDialog(root,"电脑生命归零，你赢了！");
                }
                if(people.life==0){
                    JOptionPane.showMessageDialog(root,"你生命归零，你输了！你真菜！");
                }
            }
        });
        defense.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int m_act;
                if(people.power==0&&machine.power==0){
                    m_act=0;
                }
                else {
                    m_act = new randomw().random(machine.power);
                }
                thing1.setText("你开始了防御");
                root.remove(gong);
                root.remove(xu);
                root.repaint();
                root.add(fang);
                fang.setBounds(20,200,150,150);
                root.repaint();
                while(people.power==0&&m_act==1){
                    m_act=new randomw().random(machine.power);
                }
                if(m_act==2){
                    machine.attack();
                    root.remove(fang1);
                    root.repaint();
                    root.remove(xu1);
                    root.repaint();
                    root.add(gong1);
                    root.repaint();
                    gong1.setBounds(430,200,150,150);
                    thing2.setText("电脑发起了攻击，蓄气值-1");
                    thing3.setText("电脑的冲击波被你防御，攻击无效");
                }
                else if(m_act==1){
                    machine.denfense();
                    root.remove(gong1);
                    root.repaint();
                    root.remove(xu1);
                    root.repaint();
                    root.add(fang1);
                    fang1.setBounds(430,200,150,150);
                    root.repaint();
                    thing2.setText("电脑开始了防御");
                    thing3.setText("你和电脑同时防御，相视一笑");
                }
                else if(m_act==0) {
                    machine.xuqi();
                    root.remove(gong1);
                    root.remove(fang1);
                    root.add(xu1);
                    xu1.setBounds(430,200,150,150);
                    root.repaint();
                    thing2.setText("电脑使用了蓄气，蓄气值+1");
                    thing3.setText("你防御，电脑蓄气，你错过了好时机！");
                }
                fresh();
                if(machine.life==0){
                    JOptionPane.showMessageDialog(root,"电脑生命归零，你赢了！");
                }
                if(people.life==0){
                    JOptionPane.showMessageDialog(root,"你生命归零，你输了！你真菜！");
                }
            }
        });
        xuqi.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                people.xuqi();
                thing1.setText("你使用了蓄气，蓄气值+1");
                root.remove(gong);
                root.remove(fang);
                root.add(xu);
                xu.setBounds(20,200,150,150);
                root.repaint();
                int m_act;
                if(people.power==1&&machine.power==0){
                    m_act=0;
                }
                else {
                    m_act = new randomw().random(machine.power);
                }
                if(machine.power!=0){
                    m_act=2;
                }
                if(m_act==0){
                    machine.xuqi();
                    root.remove(gong1);
                    root.remove(fang1);
                    root.add(xu1);
                    xu1.setBounds(430,200,150,150);
                    root.repaint();
                    thing2.setText("电脑使用了蓄气，蓄气值+1");
                    thing3.setText("你们都使用了蓄气，本次相安无事");
                }
                else if(m_act==1){
                    root.remove(gong1);
                    root.repaint();
                    root.remove(xu1);
                    root.repaint();
                    root.add(fang1);
                    fang1.setBounds(430,200,150,150);
                    root.repaint();
                    thing2.setText("电脑开始了防御");
                    thing3.setText("你本次小赚一笔！");
                }
                else{
                    machine.attack();
                    root.remove(fang1);
                    root.repaint();
                    root.remove(xu1);
                    root.repaint();
                    root.add(gong1);
                    root.repaint();
                    gong1.setBounds(430,200,150,150);
                    thing2.setText("电脑发起了攻击，蓄气值-1");
                    thing3.setText("电脑在你蓄气时攻击，你生命值-1");
                    people.wood();
                    if(people.life==2){
                        root.remove(ai3);
                        root.repaint();
                    }
                    if(people.life==1){
                        root.remove(ai2);
                        root.repaint();
                    }
                }
                fresh();
                if(machine.life==0){
                    JOptionPane.showMessageDialog(root,"电脑生命归零，你赢了！");
                }
                if(people.life==0){
                    root.remove(ai);
                    root.repaint();
                    JOptionPane.showMessageDialog(root,"你生命归零，你输了！你真菜！");
                }
            }
        });
    }


    private void fresh() {
         life1.setText("你的生命值"+people.life);
         power1.setText("你的蓄气值"+people.power);
         life2.setText("电脑生命值"+machine.life);
        power2.setText("电脑蓄气值"+machine.power);
    }
}
